home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / earthlink / nscomm / java40.jar / netscape / applet / Task.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-03  |  5.8 KB  |  272 lines

  1. package netscape.applet;
  2.  
  3. import java.net.URL;
  4. import java.util.Vector;
  5. import netscape.application.Application;
  6. import netscape.application.CommandEvent;
  7. import netscape.application.Event;
  8. import netscape.application.EventLoop;
  9. import netscape.util.Enumeration;
  10. import netscape.util.Hashtable;
  11. import netscape.util.InconsistencyException;
  12.  
  13. final class Task implements Runnable {
  14.    static Hashtable taskByName;
  15.    String _taskName;
  16.    TaskThreadGroup group;
  17.    Thread thread;
  18.    URL _codebaseURL;
  19.    URL _archiveURL;
  20.    AppletClassLoader _classLoader;
  21.    String _appClassName;
  22.    Application _application;
  23.    EventLoop eventLoop;
  24.    TaskOwner _taskOwner;
  25.    Vector initialEventQueue = new Vector();
  26.    boolean registered;
  27.    boolean running;
  28.    boolean dead;
  29.    boolean runLocked;
  30.  
  31.    public static void destroyAll() {
  32.       Enumeration var0 = getTasks();
  33.  
  34.       while(var0.hasMoreElements()) {
  35.          Task var1 = (Task)var0.nextElement();
  36.          var1.stop();
  37.       }
  38.  
  39.    }
  40.  
  41.    public static Task currentTask() {
  42.       for(ThreadGroup var0 = Thread.currentThread().getThreadGroup(); var0 != null; var0 = var0.getParent()) {
  43.          if (var0 instanceof TaskThreadGroup) {
  44.             return ((TaskThreadGroup)var0).task();
  45.          }
  46.       }
  47.  
  48.       return null;
  49.    }
  50.  
  51.    public static synchronized Task taskNamed(URL var0, String var1) {
  52.       return taskByName == null ? null : (Task)taskByName.get(new TaskID(var0, var1));
  53.    }
  54.  
  55.    static synchronized void registerTask(Task var0) {
  56.       if (taskByName == null) {
  57.          taskByName = new Hashtable();
  58.       }
  59.  
  60.       if (!var0.registered) {
  61.          taskByName.put(new TaskID(var0.codebaseURL(), var0.taskName()), var0);
  62.          var0.registered = true;
  63.       }
  64.    }
  65.  
  66.    static synchronized void unregisterTask(Task var0) {
  67.       if (taskByName != null && var0.registered) {
  68.          taskByName.remove(new TaskID(var0.codebaseURL(), var0.taskName()));
  69.          var0.registered = false;
  70.       }
  71.    }
  72.  
  73.    public static synchronized Enumeration getTasks() {
  74.       if (taskByName == null) {
  75.          taskByName = new Hashtable();
  76.       }
  77.  
  78.       return ((Hashtable)taskByName.clone()).elements();
  79.    }
  80.  
  81.    public Task(String var1, String var2, URL var3, URL var4, TaskOwner var5) {
  82.       this._taskName = var1;
  83.       this._appClassName = var2;
  84.       this._codebaseURL = var3;
  85.       this._archiveURL = var4;
  86.       this._taskOwner = var5;
  87.       SecurityManager.enablePrivilege("UniversalThreadAccess");
  88.       SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  89.       this.group = new TaskThreadGroup(var1, this);
  90.       this.thread = new Thread(this.group, this);
  91.       SecurityManager.revertPrivilege();
  92.       registerTask(this);
  93.    }
  94.  
  95.    public String taskName() {
  96.       return this._taskName;
  97.    }
  98.  
  99.    public URL codebaseURL() {
  100.       return this._codebaseURL;
  101.    }
  102.  
  103.    public URL archiveURL() {
  104.       return this._archiveURL;
  105.    }
  106.  
  107.    public TaskOwner taskOwner() {
  108.       return this._taskOwner;
  109.    }
  110.  
  111.    public ThreadGroup threadGroup() {
  112.       return this.group;
  113.    }
  114.  
  115.    public Application application() {
  116.       return this._application;
  117.    }
  118.  
  119.    public ClassLoader classLoader() {
  120.       if (this._classLoader != null) {
  121.          return this._classLoader;
  122.       } else if (this._codebaseURL == null) {
  123.          return null;
  124.       } else {
  125.          this._classLoader = new AppletClassLoader((MozillaAppletContext)null, this._codebaseURL, this._archiveURL);
  126.          return this._classLoader;
  127.       }
  128.    }
  129.  
  130.    public synchronized boolean isRunning() {
  131.       return this.running;
  132.    }
  133.  
  134.    public synchronized boolean isDead() {
  135.       return this.dead;
  136.    }
  137.  
  138.    public void run() {
  139.       synchronized(this){}
  140.  
  141.       try {
  142.          if (this.runLocked) {
  143.             throw new InconsistencyException("Do not run a Task more than once.");
  144.          }
  145.  
  146.          this.runLocked = true;
  147.       } catch (Throwable var30) {
  148.          throw var30;
  149.       }
  150.  
  151.       try {
  152.          this.prepareToRun();
  153.          synchronized(this){}
  154.  
  155.          try {
  156.             this.running = true;
  157.             this.moveEvents();
  158.             this.notifyAll();
  159.          } catch (Throwable var28) {
  160.             throw var28;
  161.          }
  162.  
  163.          this.didStartRunning();
  164.          this._application.run();
  165.       } finally {
  166.          synchronized(this){}
  167.  
  168.          try {
  169.             this.running = false;
  170.             this.dead = true;
  171.             this.notifyAll();
  172.          } catch (Throwable var27) {
  173.             throw var27;
  174.          }
  175.  
  176.          unregisterTask(this);
  177.          this.didStopRunning();
  178.       }
  179.  
  180.       this.stop();
  181.    }
  182.  
  183.    private void prepareToRun() {
  184.       Class var1;
  185.       try {
  186.          var1 = this.loadClass(this._appClassName);
  187.       } catch (ClassNotFoundException var6) {
  188.          ((Throwable)var6).printStackTrace(System.err);
  189.          throw new Error("I'm going under!!!");
  190.       }
  191.  
  192.       Object var2;
  193.       try {
  194.          var2 = var1.newInstance();
  195.       } catch (InstantiationException var4) {
  196.          ((Throwable)var4).printStackTrace(System.err);
  197.          throw new Error("I'm going under!!!");
  198.       } catch (IllegalAccessException var5) {
  199.          ((Throwable)var5).printStackTrace(System.err);
  200.          throw new Error("I'm going under!!!");
  201.       }
  202.  
  203.       this._application = (Application)var2;
  204.       this.eventLoop = this._application.eventLoop();
  205.       if (this._taskOwner != null) {
  206.          this._taskOwner.appDidConstruct(this._application);
  207.       }
  208.  
  209.    }
  210.  
  211.    private void didStartRunning() {
  212.    }
  213.  
  214.    private void didStopRunning() {
  215.    }
  216.  
  217.    public void start() {
  218.       this.thread.start();
  219.    }
  220.  
  221.    public void stop() {
  222.       AppletThreadList var1 = new AppletThreadList(this.group, this);
  223.       MozillaAppletContext.killer.addAppletThread(var1);
  224.    }
  225.  
  226.    public void requestShutdown() {
  227.       if (!this.dead && this._taskOwner != null) {
  228.          this._taskOwner.taskWillShutdown(this);
  229.       }
  230.  
  231.       this._classLoader = null;
  232.    }
  233.  
  234.    private void moveEvents() {
  235.       if (this.initialEventQueue != null) {
  236.          for(int var2 = 0; var2 < this.initialEventQueue.size(); ++var2) {
  237.             CommandEvent var1 = (CommandEvent)this.initialEventQueue.elementAt(var2);
  238.             this.eventLoop.addEvent(var1);
  239.          }
  240.  
  241.          this.initialEventQueue = null;
  242.       }
  243.    }
  244.  
  245.    public synchronized void addEvent(Event var1) {
  246.       if (this.eventLoop != null) {
  247.          if (this.initialEventQueue != null) {
  248.             this.moveEvents();
  249.          }
  250.  
  251.          this.eventLoop.addEvent(var1);
  252.       } else {
  253.          this.initialEventQueue.insertElementAt(var1, this.initialEventQueue.size());
  254.       }
  255.    }
  256.  
  257.    public Class loadClass(String var1) throws ClassNotFoundException {
  258.       AppletClassLoader var2 = (AppletClassLoader)this.classLoader();
  259.       return var2 == null ? Class.forName(var1) : var2.loadClass(var1);
  260.    }
  261.  
  262.    public synchronized void waitUntilRunningOrDead() {
  263.       while(!this.isRunning() && !this.isDead()) {
  264.          try {
  265.             this.wait();
  266.          } catch (InterruptedException var1) {
  267.          }
  268.       }
  269.  
  270.    }
  271. }
  272.